/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.leaveMsg.controller;

import com.fzhucc.annotation.AnonymousAccess;
import com.fzhucc.annotation.Log;
import com.fzhucc.modules.leaveMsg.domain.LeaveMsg;
import com.fzhucc.modules.leaveMsg.domain.dto.LeaveMsgAddDto;
import com.fzhucc.modules.leaveMsg.domain.dto.LeaveMsgEditDto;
import com.fzhucc.modules.leaveMsg.service.LeaveMsgService;
import com.fzhucc.modules.leaveMsg.domain.dto.LeaveMsgQueryCriteria;
import com.fzhucc.modules.sciCxResult.domain.dto.SciCxResultQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
 * @author yyg
 * @date 2024-01-11
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "职工感悟:管理")
@RequestMapping("/api/leaveMsg")
public class LeaveMsgController {

    private final LeaveMsgService leaveMsgService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('leaveMsg:list')")
    public void exportLeaveMsg(HttpServletResponse response, LeaveMsgQueryCriteria criteria) throws IOException {
        leaveMsgService.download(leaveMsgService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询职工感悟")
    @ApiOperation("查询职工感悟")
    @PreAuthorize("@el.check('leaveMsg:list')")
    public ResponseEntity<Object> queryLeaveMsg(LeaveMsgQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(leaveMsgService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    @AnonymousAccess
    @GetMapping(value = "/list")
    @Log("h5、小程序查询职工感悟")
    @ApiOperation("h5、小程序查询职工感悟")
    public ResponseEntity<Object> list(LeaveMsgQueryCriteria criteria, Pageable pageable) {
        criteria.setIsShow(1);
        return new ResponseEntity<>(leaveMsgService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @GetMapping(value = "/my")
    @Log("查询我的职工感悟")
    @ApiOperation("查询我的职工感悟")
    public ResponseEntity<Object> my(LeaveMsgQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(leaveMsgService.my(criteria, pageable), HttpStatus.OK);
    }

    @AnonymousAccess
    @PostMapping
    @Log("新增职工感悟")
    @ApiOperation("新增职工感悟")
    //@PreAuthorize("@el.check('leaveMsg:add')")
    public ResponseEntity<Object> createLeaveMsg(@Validated @RequestBody LeaveMsgAddDto resources){
        return new ResponseEntity<>(leaveMsgService.create(resources), HttpStatus.CREATED);
    }

    @AnonymousAccess
    @PutMapping
    @Log("修改职工感悟")
    @ApiOperation("修改职工感悟")
    //@PreAuthorize("@el.check('leaveMsg:edit')")
    public ResponseEntity<Object> updateLeaveMsg(@Validated @RequestBody LeaveMsgEditDto resources){
        leaveMsgService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除职工感悟")
    @ApiOperation("删除职工感悟")
    @PreAuthorize("@el.check('leaveMsg:del')")
    public ResponseEntity<Object> deleteLeaveMsg(@RequestBody Integer[] ids) {
        leaveMsgService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}